package com.buba.system.controller;

import com.alibaba.fastjson.JSON;
import com.buba.system.pojo.LoginLog;
import com.buba.system.service.LoginLogService;
import com.buba.system.service.impl.LoginLogServiceImpl;
import com.buba.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/LoginLogServlet")
public class LoginLogServlet extends BaseServlet {
    LoginLogService loginLogService = new LoginLogServiceImpl();
    @Override
    public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String flag = request.getParameter("flag");
        if ("selLogin".equals(flag))
            this.selLogin(request,response);
        if ("delLogin".equals(flag))
            this.delLogin(request,response);

    }

    private void delLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("删除");
        String loginId = request.getParameter("loginId");
        int count = loginLogService.delLogin(loginId);
        response.getWriter().write(count+"");
    }

    private void selLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> param = getParam(request);
//        for(String key:param.keySet()){
//            System.out.println("key:"+key+" "+"Value:"+param.get(key));
//        }
        List<LoginLog> list = loginLogService.selLogin(param);
        response.getWriter().write(JSON.toJSONString(list));
    }
}
